﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.Tools
{
    public partial class 常用工具 : System.Web.UI.Page
    {
        static CommonLib.DbHelper.IDBhelper DbHelper = new CommonLib.DbHelper.Factory().IDBhelper;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //try
                //{
                //    using (TransactionScope scope = new TransactionScope())
                //    { 
                //        ID guid = ID.NewGuid();
                //        DBContext.ExTranAdd(new EntityModel.Base_Drill()
                //        {
                //            Title = "111"
                //        });
                //        DBContext.ExTranAdd(new EntityModel.Base_Drill()
                //        {
                //            ID = guid,
                //            Title = "222"
                //        });
                //        DBContext.ExTranAdd(new EntityModel.Base_Drill()
                //        {
                //            ID = guid,
                //            Title = "3333"
                //        });
                //        DBContext.SaveChanges();
                //        scope.Complete();
                //    }
                //}
                //catch (Exception ex)
                //{

                //} 
            }
        }

        protected void 新增表Button_Click(object sender, EventArgs e)
        {
            //var tableList = LambdaToSql.FrameWork.Tools.GetDataTable().Select(ex => ex.Name).ToList();
            //var tableListServer = LambdaToSql.FrameWork.Tools.GetDataTable("ConnectionString1").Select(ex => ex.Name).ToList();

            //foreach (var item in tableList)
            //{
            //    if (!tableListServer.Contains(item))
            //    {
            //        var name = string.Format("dbo.{0}.Table.sql", item);
            //        try
            //        {
            //            System.IO.File.Copy(@"D:\data\all\" + name, @"D:\data\update\" + name);
            //        }
            //        catch
            //        {
            //        }
            //        Response.Write(item + "<br />");
            //    }
            //}
        }

        protected void 新增字段Button_Click(object sender, EventArgs e)
        {
            //var tableList = Tools.GetDataTable();
            //var tableListServer = FrameWork.Tools.GetDataTable("ConnectionString1");

            //foreach (var item in tableList)
            //{
            //    var str = "<b>" + item.Name + "</b>&nbsp;&nbsp;{0}";
            //    var col = "";
            //    foreach (var columns in item.Columns)
            //    {
            //        try
            //        {
            //            if (!tableListServer.Where(ex => ex.Name == item.Name).FirstOrDefault().Columns.Select(ex => ex.Name).ToList().Contains(columns.Name))
            //            {
            //                col += columns.Name + "<br />";
            //            }
            //        }
            //        catch
            //        {
            //        }
            //    }
            //    if (col != "")
            //    {
            //        Response.Write(string.Format(str, col) + "<br /><br /><br />");
            //        Response.Flush();
            //    }
            //}
        }

        /// <summary>
        /// 生成配置文件类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            //var str = "";
            //var list = DBContext.ExSelect<EntityModel.System_Dictionary>().ToList().OrderBy(ex => ex.ID);
            //foreach (var item in list)
            //{
            //    var temp = "/// <summary>\r\n";
            //    temp += "/// {0}\r\n";
            //    temp += "/// </summary>\r\n";
            //    temp += "[DataMember(Name = \"{1}\", IsRequired = true, Order = {2})]\r\n";
            //    temp += "public static ID {1};\r\n\r\n";
            //    str += string.Format(temp, item.Name, item.Keys, item.Sort);
            //}
            //Response.Write(str);
            //Response.End();
        }

        /// <summary>
        /// 生成静态字典JS文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            //EntityModel.DBModel DBContext = new EntityModel.DBModel();
            //var list = DBContext.ExSelect<EntityModel.System_Dictionary>().OrderBy(ex => ex.Sort).ToList();

            //StringBuilder str = new StringBuilder();
            //str.Append("var DicType ={\r\n");
            //foreach (var item in list)
            //{
            //    var ls = ",";
            //    if (list.IndexOf(item) == (list.Count - 1))
            //    {
            //        ls = "";
            //    }
            //    str.Append(string.Format("//{0}\r\n{1}: \"{2}\"{3}\r\n", item.Name, item.Keys, item.Value, ls));
            //}
            //str.Append("}");
            //Response.Write(str.ToString());
            //Response.End();
        }

        protected void Button3_Click(object sender, EventArgs e)//新增系统字典
        {
            var listid = DbHelper.ExecuteScalars("SELECT  '''' + CONVERT(varchar(64), ID) + '''' AS Expr1 FROM System_Category", "ConnectionString1");
            var listid1 = DbHelper.ExecuteScalars("SELECT  '''' + CONVERT(varchar(64), ID) + '''' AS Expr1 FROM System_Category where guid not in (" + listid + ")");

            if (listid1 != "")
            {
                Literal1.Text = "SELECT  * FROM System_Category where guid in (" + listid1 + ") order by Levels";
            }
            else
            {
                Literal1.Text = "没有新增";
            }
        }

        protected void Button4_Click(object sender, EventArgs e)//新增静态字典
        {
            var listid = DbHelper.ExecuteScalars("SELECT  '''' + CONVERT(varchar(64), ID) + '''' AS Expr1 FROM System_Dictionary", "ConnectionString1");
            var listid1 = DbHelper.ExecuteScalars("SELECT  '''' + CONVERT(varchar(64), ID) + '''' AS Expr1 FROM System_Dictionary where guid not in (" + listid + ")");

            if (listid1 != "")
            {
                Literal1.Text = "SELECT  * FROM System_Dictionary where guid in (" + listid1 + ") order by Sort";
            }
            else
            {
                Literal1.Text = "没有新增";
            }
        }

        protected void Button5_Click(object sender, EventArgs e)//新增目录
        {
            var listid = DbHelper.ExecuteScalars("SELECT  '''' + CONVERT(varchar(64), ID) + '''' AS Expr1 FROM System_Menu", "ConnectionString1");
            var listid1 = DbHelper.ExecuteScalars("SELECT  '''' + CONVERT(varchar(64), ID) + '''' AS Expr1 FROM System_Menu where guid not in (" + listid + ")");

            if (listid1 != "")
            {
                Literal1.Text = "SELECT  * FROM System_Menu where guid in (" + listid1 + ") order by Levels";
            }
            else
            {
                Literal1.Text = "没有新增";
            }
        }
    }
}